# coding=utf-8
import numpy as np
import click


class Bid:
    BID_LIMIT = 5
    MAX_PRICE = 11000

    def __init__(self, max_val, min_val, max_range, min_range,
                 num=10, score=60, max_iter=100000):
        """

        :param max_val: 最高投标价, 每次生成的投标数据中包含大于此值视为作废
        :param min_val: 最低投标价, 每次生成的投标数据中包含小于此值视为作废
        :param max_range: 合理的投标上限
        :param min_range: 合理的投标下限
        :param num: 投标厂商数目
        :param score: 投标得分
        :param max_iter: 最大迭代次数
        """
        self.max_val = max_val
        self.min_val = min_val
        self.max_range = max_range
        self.min_range = min_range
        self.num = num
        self.score = score
        self.max_iter = max_iter
        self.base_price = 0
        self.changed = False

    def single_fit(self):
        avg = (self.max_range + self.min_range) / 2
        scale = (self.max_range - self.min_range) / 2
        prices = np.random.normal(avg, scale, self.num)
        prices = np.sort(prices)

        if not self.changed:
            if self.num > self.BID_LIMIT:
                prices = prices[1:self.num - 1]
                self.num = self.num - 2
                self.changed = True
                if self.num != len(prices):
                    raise Exception(
                        'Not Equal: {} != {}'.format(self.num, len(prices)))

        avg_price = np.average(prices)
        self.base_price = avg_price * 0.85
        filtered_price = prices[np.where(self.base_price <= prices)]
        filtered_price = filtered_price[np.where(filtered_price <= self.MAX_PRICE)]
        # print('base: {}, prices: {}, filtered_price: {}'.format(self.base_price, prices, filtered_price))
        # 正态分布, 去掉不合理的投标数据
        # index = np.where(filtered_price < self.min_val)
        # if len(index[0]) > 0:
        #     return
        # index = np.where(filtered_price > self.max_val)
        # if len(index[0]) > 0:
        #     return
        return filtered_price[0]

    def fit(self):
        winner = np.zeros(self.max_iter)
        i = 0
        while i < self.max_iter:
            price = self.single_fit()
            if price is None:
                continue
            else:
                winner[i] = price
                i += 1
            # print(i)
        return winner


@click.command()
@click.option('--max_iter')
@click.option('--num')
def main(max_iter, num):
    max_iter = int(max_iter)
    num = int(num)
    print(max_iter)
    b = Bid(8800, 6900, 10500, 7200, num=num, max_iter=max_iter)
    winner = b.fit()
    print(u'投标厂商数目 {}, 中标价平均值 {:.2f}, 最大值 {:.2f}, 最小值 {:.2f}, 标准差 {:.2f};'.format(
        num, np.mean(winner), np.max(winner), np.min(winner), np.std(winner)))

if __name__ == '__main__':
    main()
